package com.espirit.hr;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.espirit.eap.manager.Department;
import com.espirit.eap.sa.Employee;
import com.googlecode.cswish.struts.spring.GenericService;

@Service
public class EmployeeService {
	@Resource
	GenericService genericService;
	
	@Resource
	SalaryAcctItemService salaryAccountBookStandardService;
	
	public List<SalaryAcctItem> getSalaryAccountBookStandards(Integer empId) {
		Employee emp = genericService.load(Employee.class, empId);
		if (emp == null || emp.getDepartment() == null) return Collections.EMPTY_LIST;
		
		Integer deptId = emp.getDepartment().getId();
		List<SalaryAcctItem> validStandards = salaryAccountBookStandardService.getValidSalaryAccountStands();
		if (validStandards == null) return Collections.EMPTY_LIST;
		List<SalaryAcctItem> results = null;
		
		for(SalaryAcctItem sabs : validStandards){
			String deptIds = sabs.getDeptIds();
			if (!StringUtils.isEmpty(deptIds)){
				String[] ids = deptIds.split(",");
				for(String id : ids){
					Department dept = genericService.load(Department.class, Integer.valueOf(id));
					List<Integer> deptIdList = dept.getDescDepartmentIdList();
					if (deptIdList.contains(deptId)){
						if (results == null){
							results = new ArrayList<SalaryAcctItem>();
						}
						results.add(sabs);
					}
				}
			}
		}
		return results == null ? Collections.EMPTY_LIST : results;
	}

}
